<?php
/*
		incase we wana cache the files we are getten ...
*/
class cache {

	var $DIR_cache='cache/';
		var $loading_bar = 50;								 // How many characters in the loading bar
	var $loading_bar_mask = "=";
	var $current_file;
	protected $methods_allowed = array(
		'cache',
	);
	public function cachejsonfile($filename, $jsonstream,$fields=null) 
	{
		global $config;
		$fd = str_replace('?fields=', "", $fields);
		$filename = str_replace('/', "-", $filename);
		$json_filename = $this->DIR_cache.$filename.$fd.'_'.time().'.json';

		$handle = fopen($json_filename, "w+");
		$numbytes = fwrite($handle, $jsonstream);
		fclose($handle);
		$url_filesize_obj = $this->fileSizeInfo($numbytes);
		return true;
	}
	
	public function getCacheFileInfo($filename,$fields)
	{
		global $roster;
		// we are not making files any more
		$day = (1 * 24 * 60 * 60);// one day in time()
		$sqlquery2 = "SELECT dateupdatedutc FROM `api_players` WHERE `name` = '" . $fields['name'] . "' AND `server` = '" . $fields['server'] . "'";
		$result2 = $roster->db->query($sqlquery2);
		$rows = $roster->db->num_rows($result2);

		if ($rows == 1)
		{
			$row = $roster->db->fetch($result2);
			$date = ($row['dateupdatedutc'] + $day);
			if ($date > time())
			{
				$a = array(
						'msg' => '<font color="green">File is old updateing</font><br>',
						'result' => true,
						'file' => ''
					);
					return $a;
			}
			else
			{
					$f = $date;
					$utc_str = gmdate("M d Y H:i:s", $f);
					$utc = date("M d Y H:i:s", $f);                 //strtotime($utc_str);

					$a = array(
						'msg' => '<font color="red">Your Profile is less then 1 day old update will be @ "'.$utc.'"</font><br>',
						'result' => false,
					);
					return $a;
					break;
			}
		}
		else
		{
			$a = array(
				'msg' => '<font color="green">No profile Found</font><br>',
				'result' => true,
				'file' => ''
			);
			return $a;
			break;
		}
	}
	
	public function getCacheFile( $XMLtype ) {
	global $config;
	$caschmsg = '';
	echo $XMLtype.' xmltype<br>';
	$new_file_size = 0;
	$new_file_time = 0;
	$new_file_name = "";
	$old_file_date = (1 * 24 * 60 * 60);
	$dir = opendir ($this->DIR_cache);
	$a=array();
	$filecount = count(glob($this->DIR_cache."*.json"));
	$filecount_current=0;
	$loading_bar_list = array();
	if ($filecount == 0)
	{
		$a = array(
			'msg' => '<font color="green">No file</font><br>',
			'result' => true,
			'file' => ''
		);
		return $a;
		break;
	}
	 while (($file = readdir($dir)) !== false)
	{$r=0;
		if (strpos($file, '.json',1))
		{

		$filecount_current++;
			list($type,$date) = explode("_",$file);
			if ($type ==  $XMLtype)
			{

				//echo '=='.$file.'<br>';
				list($type,$date) = explode("_",$file);
				$date = str_replace('.json', "", $date);
				$e = time();
				$x = ($old_file_date + $date);
				if ($x > $e)
				{
					$f = $date + $old_file_date;
					$this->current_file = $this->DIR_cache.$file;
					$utc_str = gmdate("M d Y H:i:s", $f);
					$utc = date("M d Y H:i:s", $x);                 //strtotime($utc_str);

					$a = array(
						'msg' => '<font color="red">Your Profile is less then 1 day old update will be @ "'.$utc.'"</font><br>',
						'result' => false,
						'file' => $this->DIR_cache.$file
					);
					return $a;
					break;

				}
				elseif ($x < $e) 
				{
					
					$a = array(
						'msg' => '<font color="green">File is old updateing</font><br>',
						'result' => true,
						'file' => ''
					);
					unlink($this->DIR_cache.$file);
					return $a;
				}
			}
			elseif ($filecount_current == $filecount) 
			{
					$a = array(
						'msg' => '<font color="green">No Cach file found</font><br>',
						'result' => true,
						'file' => ''
					);
				return $a;
				//break;
			}
		}
	}
}



	function fileSizeInfo($size)
	{
		$i=0;
		$iec = array("b", "kb", "mb", "gb", "tb", "pb", "eb", "zb", "yb");
		while (($size/1024)>1) 
		{
			$size=$size/1024;
			$i++;
		}
		return substr($size,0,strpos($size,'.')+4).$iec[$i];
	}



}





?>